package org.he.auth.controller;

import org.he.auth.domain.Menu;
import org.he.auth.service.IRoleMenuService;
import org.he.auth.domain.RoleMenu;
import org.he.auth.query.RoleMenuQuery;
import org.he.basic.data.ContextMap;
import org.he.basic.util.AjaxResult;
import org.he.basic.util.PageInfo;
import org.he.org.domain.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/roleMenu")
public class RoleMenuController {
    @Autowired
    public IRoleMenuService roleMenuService;


    /**
     * 保存和修改公用的
     * @param roleMenu 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult save(@RequestBody RoleMenu roleMenu) {
        try {
            if ( roleMenu.getId() != null)
                roleMenuService.update(roleMenu);
            else
                roleMenuService.insert(roleMenu);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("保存对象失败！" + e.getMessage());
        }
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
                roleMenuService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("删除对象失败！" + e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public AjaxResult queryById(@PathVariable("id") Long id) {
        try {
            RoleMenu roleMenu =roleMenuService.queryById(id);
            return AjaxResult.success(roleMenu);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取一个失败！" + e.getMessage());
        }
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public AjaxResult queryAll() {

        try {
            List< RoleMenu> list = roleMenuService.queryAll();
            return AjaxResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取所有失败！" + e.getMessage());
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public AjaxResult queryPage(@RequestBody RoleMenuQuery query) {
        try {
            PageInfo<RoleMenu> page = roleMenuService.queryPage(query);
            return AjaxResult.success(page);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取分页数据失败！" + e.getMessage());
        }
    }

    /**
    * 批量删除
    * @param ids
    * @return
    */
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
        try {
            roleMenuService.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("批量删除失败！" + e.getMessage());
        }
    }

    @GetMapping("/tree/{token}")
    public AjaxResult queryMenuTree(@PathVariable("token") String token) {
        try {
            Employee employee = ContextMap.loginMap.get(token);
            List<Menu> menus =roleMenuService.queryMenuTree(employee.getId());
            return AjaxResult.success(menus);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取失败！" + e.getMessage());
        }
    }
}
